package 剑指Offer;

import java.util.Stack;

public class Offer31_栈的压入弹出序列 {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int i = 0;//记录popped数组下标
        for (int num : pushed) {
            stack.push(num);
            while (!stack.isEmpty() && stack.peek() == popped[i]) { // 循环判断与出栈
                stack.pop();
                i++;
            }
        }
        //stack为空代表可以按照popped顺序出栈
        return stack.isEmpty();
    }
}
